function [varargout] = param_edit(cmd)
%
% function to edit parameters in control list.
% invoked when multiple selections and "edit".
%
% creates a new window (like proto_select), with a drop down list of
% available fields in the current control list (alphabetized).
% If the current values are all the same, displays the value, otherwise
% says "mixed".
% Offers a field for the new value, and takes it.
% Update button causes value to be loaded into control structure, and structure
% is saved.
% Exit causes exit.
%
global CONTROL
nbufs = 10; % fixed value -= what is in the gui?

if(nargin == 0)
    hp = findobj('tag', 'ParamEdit');
    if(isempty(hp))
        open('param_edit_fig.fig');
        param_edit('renew');
        return;
    else
        figure(hp);
        param_edit('renew');
        return;
    end;
end;
if(nargout > 0)
    for i = 1:nargout
        varargout{i} = [];
    end;
end;

switch(cmd)
    case 'renew'
        FN = fieldnames(CONTROL);
        index=[];
        not = [];
        for i = 1:length(FN)
            n = FN{i};
            cm = sprintf('CONTROL(1).%s', char(n));
            x=eval(cm);
            if(~isstruct(x))
                index = [index i];  %#ok<AGROW>
            else
                not = [not i]; %#ok<AGROW>
            end;
        end;
        FNN=FN(index);
        FNN = sort(FNN);
        hl = findobj('tag', 'paredit_list');
        oldval = get(hl, 'value');
        oldstr = get(hl, 'string');
        set(hl, 'String', FNN);
        if(length(FNN) >= length(oldval))
            if(strcmp(oldstr{oldval}, FNN{oldval}))
                set(hl, 'value', oldval);
            else
                set(hl, 'value', 1);
            end;
        else
            set(hl, 'value', 1);
        end;
        param_edit('getvalue');
        hn = findobj('tag', 'paredit_new');
        ho = findobj('tag', 'paredit_old');
        set(hn, 'String', get(ho, 'string'));
        return;
        
    case 'getvalue'
        h = findobj('tag', 'paredit_list');
        v = get(h, 'value'); % find entry;
        s = get(h, 'string');
        parval = s{v};
        hv = findobj('tag', 'paredit_old');
        hn = findobj('tag', 'paredit_new');
        hc = findobj('tag', 'CTL');
        sel = get(hc, 'value');
        if(length(sel) == 1)
            cm = sprintf('CONTROL(%d).%s', sel, parval);
            set(hv, 'string', eval(cm));
            set(hn, 'string', eval(cm));
            if(nargout >= 1)
                varargout{1} = eval(cm);
            end;
            if(nargout >= 2)
                varargout{2} = parval;
            end;
            %   fprintf('single\n');
            return;
        else
            j = 1;
            cm = cell(length(sel));
            for i = sel
                cm{j} = sprintf('CONTROL(%d).%s', i, parval);
                j = j + 1;
            end;
            for i = 2:length(cm)
                str1 = num2str(eval(cm{1}));
                str2 = num2str(eval(cm{i}));
                if(~strcmp(str1, str2))
                    cm = 'mixed values';
                    set(hv, 'string', cm);
                    set(hn, 'string', cm);
                    %            fprintf('multiple different');
                    return;
                end;
            end;
            set(hv, 'string', eval(cm{1})); % all are same.
            set(hn, 'string', eval(cm{1}));
            %     fprintf('multiple same\n');
            if(nargout >= 1)
                varargout{1} = eval(cm{1});
            end;
            if(nargout >= 2)
                varargout{2} = parval;
            end;
            
        end;
        
    case 'copy1'
        [value, field] = param_edit('getvalue');
        docopy(1, value, field);        
    case 'copy2'
        [value, field] = param_edit('getvalue');
        docopy(2, value, field);
    case 'copy3'
        [value, field] = param_edit('getvalue');
        docopy(3, value, field);
    case 'copy4'
        [value, field] = param_edit('getvalue');
        docopy(4, value, field);
    case 'copy5'
        [value, field] = param_edit('getvalue');
        docopy(5, value, field);
     case 'copy6'
        [value, field] = param_edit('getvalue');
        docopy(6, value, field);
     case 'copy7'
        [value, field] = param_edit('getvalue');
        docopy(7, value, field);
     case 'copy8'
        [value, field] = param_edit('getvalue');
        docopy(8, value, field);
     case 'copy9'
        [value, field] = param_edit('getvalue');
        docopy(9, value, field);
     case 'copy10'
        [value, field] = param_edit('getvalue');
        docopy(10, value, field);
    
    case 'save'
        PE=getbufs(nbufs); %#ok<NASGU>
        [fn, pn, fi] = uiputfile('ParamFiles/*.mat', 'Save Paramedit file');
        if(fi == 0)
            return;
        end;
        save([pn fn], 'PE');
        
    case 'open'
        param_edit('clearall'); % first clear the unused buffers to avoid confusion
        [fn, pn, fi] = uigetfile('ParamFiles/*.mat', 'Read Param_edit File');
        if(fi == 0)
            return;
        end;
        p = load([pn fn]);
        PE=p.PE;
        for i = 1:length(PE.value)
            docopy(i, PE.value{i}, PE.field{i});
        end;
        
    case 'clearall'
        value = [];
        field = [];
        for i = 1:nbufs
            docopy(i, value, field);
        end;
        
    case 'pasten'
        h=gcbo;
        s=get(h, 'string');
        indx = sscanf(s(7), '%d');
        dopaste(indx);
        
    case 'pasteall'
        for i = 1:nbufs
            dopaste(i);
        end;
        
    case 'update'
        h = findobj('tag', 'paredit_list');
        v = get(h, 'value'); % find entry;
        s = get(h, 'string');
        s1 = s{v};
%        hv = findobj('tag', 'paredit_old');
        hc = findobj('tag', 'CTL');
        sel = get(hc, 'value');
        hn = findobj('tag', 'paredit_new');
        for i = sel
            %setfield(c(i), s1, get(hn, 'string'));
            eval(sprintf('CONTROL(i).%s = ''%s'';', s1, get(hn, 'string')));
            %  fprintf('%d  %s = %s\n', i, s1, get(hn, 'string'));
        end;
        
        setlist(getmainselection);
        param_edit('getvalue'); % update information on display
        
        
        
    case {'close', 'exit'}
        h = findobj('tag', 'ParamEdit');
        close(h);
        return;
        
    otherwise
end;

function docopy(i, value, field)
buf = sprintf('pastebuf%d', i);
feld = sprintf('pastefield%d', i);
hp = findobj('tag', buf);
hf = findobj('tag', feld);
set(hp, 'String', value);
set(hf, 'String', field);

function [PE] = getbufs(nbufs)
for i = 1:nbufs
    buf = sprintf('pastebuf%d', i);
    feld = sprintf('pastefield%d', i);
    hp = findobj('tag', buf);
    hf = findobj('tag', feld);
    PE.value{i} = get(hp, 'String');
    PE.field{i} = get(hf, 'String');
end;


function dopaste(n)

global CONTROL %#ok<NUSED>

h = findobj('tag', sprintf('pastefield%d', n));
pastename = get(h, 'string'); % field name

if(isempty(pastename)) % skip empty fields (they are cleared)
    return;
end;

sf = getmainselection;
if(isempty(sf))
    return;
end;
hn = findobj('tag', sprintf('pastebuf%d', n));

pastepar = get(hn, 'string');

for i = sf
    eval(sprintf('CONTROL(i).%s = ''%s'';', pastename, pastepar));
    fprintf(1, 'buf %d to list# %d,  %s = %s\n', n, i, pastename, pastepar);
end;

setlist(getmainselection);
param_edit('getvalue'); % update information on display
return;

